﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Windows.Data;
using DataFileApplication_WPF.Extensions;

namespace DataFileApplication_WPF.Common
{
    public class ValueToComboboxConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {



            return value == null || string.IsNullOrEmpty(value?.ToString()) ? string.Empty : value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            EnumDataType type = (EnumDataType)Enum.Parse(typeof(EnumDataType), parameter.ToString());
            switch (type)
            {
                case EnumDataType.Boolean:
                    if (bool.TryParse(value?.ToString(), out bool boolValue))
                    {
                        return boolValue;
                    }
                    return false;
                case EnumDataType.String:
                    return value?.ToString();
                case EnumDataType.Integer:
                    if (int.TryParse(value?.ToString(), out int intValue))
                    {
                        return intValue;
                    }
                    return 0;
                case EnumDataType.Double:
                    if (double.TryParse(value?.ToString(), out double doubleValue))
                    {
                        return doubleValue;
                    }
                    return 0.0;
                //case EnumDataType.Float:
                //    if (float.TryParse(value?.ToString(), out float dateTimeValue))
                //    {
                //        return dateTimeValue;
                //    }
                //    return 0.0f;
                default:
                    return value?.ToString();


            }
        }
    }
}
